const Number = require("../model/number.model");
class NumberService {
  /**
   * 创建号码数据
   * @param {*} param0
   * @returns
   */
  async createNumber({ numberName, numberNumber }) {
    const res = await Number.create({
      numberName,
      numberNumber,
    });
    return res ? res.dataValues : null;
  }

  /**
   * 更新号码数据
   * @param {*} param0
   * @returns
   */
  async updateNumber(id, data) {
    const res = await Number.update(data, { where: { id } });
    return res[0] > 0;
  }

  /**
   * 下架号码数据
   * @param {*} param0
   * @returns
   */
  async removeNumber(id) {
    const res = await Number.destroy({ where: { id } });
    return res > 0;
  }

  /**
   * 上架号码数据
   * @param {*} param0
   * @returns
   */
  async restoreNumber(id) {
    const res = await Number.restore({ where: { id } });
    return res > 0;
  }

  /**
   * 获取号码列表
   * @param {*} page
   * @param {*} pageSize
   * @returns
   */
  async findNumber(page, pageSize) {
    const offset = (page - 1) * pageSize;
    const limit = pageSize * 1;
    const { count,rows} = await Number.findAndCountAll({
      offset,limit
    })
    return {
      page,
      pageSize,
      total: count,
      list: rows,
    };
  }

  /**
   * 获取用户信息
   * @param {*} param0
   * @returns
   */
  async getNumberInfo({ id, numberName, numberNumber }) {
    const whereOpt = {};
    id && Object.assign(whereOpt, { id });
    numberNumber && Object.assign(whereOpt, { numberNumber });
    numberName && Object.assign(whereOpt, { numberName });

    const res = await Number.findOne({
      attributes: ["id", "numberNumber", "numberName"],
      where: whereOpt,
    });
    return res ? res.dataValues : null;
  }
}

module.exports = new NumberService();
